package de.barny.obj;

import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.List;

import de.barny.gui.GameField;
import de.barny.prefs.ResourceBundleHelper;

public class Snake {

	public List<Dot> dots = new LinkedList<Dot>();

	char dir = 'r';
	char oldDir = 'r';

	int playerN;

	boolean collided = false;
	
	Color dotColor;

	public Snake(int d, int player) {
		playerN = player;
		
		if (player == 1) {
			dotColor = new Color(200,210,220);
		} else if (player == 2) {
			dotColor = new Color(200,255,180);
		}
		initSnake(d);
	}

	public Snake(int d) {
		initSnake(d);
	}

	public void initSnake(int n) {
		dots.clear();

		if (playerN == 1) {
			dir = 'r';
			oldDir = 'r';

			for (int i = 1; i <= n; i++) {
				dots.add(new Dot(i, 2, dotColor));
			}
		} else if (playerN == 2) {
			dir = 'l';
			oldDir = 'l';

			int windowWidth = Integer.parseInt(getResource("window.size.width"));
			int windowHeight = Integer.parseInt(getResource("window.size.height"));
			int dotSize = Integer.parseInt(getResource("snake.dot.size"));

			int possibilitiesX = windowWidth/dotSize;
			int possibilitiesY = windowHeight/dotSize;

			for (int i = 1; i <= n; i++) {
				dots.add(new Dot(possibilitiesX-i, possibilitiesY-2, dotColor));
			}
		} else {
			System.out.println("Spielerzahl nicht definiert");
		}
	}

	public void paint(Graphics gameFieldGraphics) {
		Dot lastDot = getLastDot();
		if (dir == 'r' && oldDir != 'l') {
			dots.remove(0);
			dots.add(new Dot(lastDot.getPosX()+1, lastDot.getPosY(), dotColor));
		} else if (dir == 'l' && oldDir != 'r') {
			dots.remove(0);
			dots.add(new Dot(lastDot.getPosX()-1, lastDot.getPosY(), dotColor));
		} else if (dir == 'u' && oldDir != 'd') {
			dots.remove(0);
			dots.add(new Dot(lastDot.getPosX(), lastDot.getPosY()-1, dotColor));
		} else if (dir == 'd' && oldDir != 'u') {
			dots.remove(0);
			dots.add(new Dot(lastDot.getPosX(), lastDot.getPosY()+1, dotColor));
		} else {
			if (dir == 'r' && oldDir == 'l') {
				dots.remove(0);
				dots.add(new Dot(lastDot.getPosX()-1, lastDot.getPosY(), dotColor));
			} else if (dir == 'l' && oldDir == 'r') {
				dots.remove(0);
				dots.add(new Dot(lastDot.getPosX()+1, lastDot.getPosY(), dotColor));
			} else if (dir == 'u' && oldDir == 'd') {
				dots.remove(0);
				dots.add(new Dot(lastDot.getPosX(), lastDot.getPosY()+1, dotColor));
			} else if (dir == 'd' && oldDir == 'u') {
				dots.remove(0);
				dots.add(new Dot(lastDot.getPosX(), lastDot.getPosY()-1, dotColor));
			}
		}

		for (Dot dot: dots) {
			dot.paint(gameFieldGraphics);
		}
	}

	public void checkCollision() {
		for (Dot dot: dots) {
			if (getLastDot().getPosX() == dot.getPosX() && getLastDot().getPosY() == dot.getPosY() && getLastDot() != dot) {
				GameField.collided = true;
			}
		}
		if (getLastDot().getPosX() <= 0 || getLastDot().getPosY() <= 0 || getLastDot().getPosX() >= (Integer.parseInt(getResource("window.size.width"))/Integer.parseInt(getResource("snake.dot.size"))) || getLastDot().getPosY() >= (Integer.parseInt(getResource("window.size.height"))/Integer.parseInt(getResource("snake.dot.size")))) {
			GameField.collided = true;
		}
	}

	public void addDot() {
		for (int i = 0; i < (Integer.parseInt(getResource("apple.carbonhydrates"))); i++) {
			dots.add(0, new Dot(0,0, dotColor)); //Der Dot wird hier auf (0, 0) gesetzt, da er sowieso gleich wieder entfernt wird.
		}
	}

	public void setDirection(char d) {
		if (d != dir) {
			oldDir = dir;
		}
		dir = d;
	}

	public Dot getLastDot() {
		return dots.get(dots.size()-1);
	}

	public List<Dot> getDots() {
		return dots;
	}

	private String getResource(String a_strResourceKey) {
		return ResourceBundleHelper.getResource(a_strResourceKey);
	}

}
